using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using MosziNET.Web.Gallery.Providers;
using System.Globalization;

namespace MosziNET.Web.Gallery
{
    /// <summary>
    /// Contains methods for setting up the gallery
    /// </summary>
    public static class GalleryManager
    {
        #region / Private fields /

        // the name of the thumbnail handler
        private static string imageHandlerFile = "Gallery.ImageHandler.axd";

        // cache or not the thumbnails
        private static bool cacheThumbnails = true;
        
        // The storage provider used for accessin the images
        private static GalleryStorageProvider storageProvider = new HybridGalleryStorageProvider();

        // the CSS class used for displaying a category thumbnail
        private static string categoryThumbClass;
        // the CSS class used for displaying an image thumb
        private static string imageThumbClass;
        // the CSS class for the main table
        private static string mainTableClass;
        // the CSS class for the main cell
        private static string mainCellClass;
        // Specified the format to be used for the url when constructing
        // the folder link
        private static string folderUrlFormat = "{0}";
        // Specifies the format for the image links
        private static string imageLinkUrlFormat = "{0}/{1}";
        // The width of the preview image
        private static int previewImageWidth = 350;
        // 
        private static string navigationTableClass;
        private static string imageTableClass;

        private static string cacheFolder = "Gallery/_cache";

        private static string adminRoleName = "admin";
        private static string cssPrefixName = "gallery";

        #endregion / Private fields /

        #region / Public properties /

        /// <summary>
        /// Gets/sets the css prefix name for the gallery
        /// </summary>
        public static string CssPrefixName
        {
            get
            {
                return cssPrefixName;
            }
            set
            {
                cssPrefixName = value;
            }
        }
        /// <summary>
        /// Gets/sets the admin role name
        /// </summary>
        public static string AdminRoleName
        {
            get
            {
                return adminRoleName;
            }
            set
            {
                adminRoleName = value;
            }
        }

        /// <summary>
        /// Gets/sets the folder for caching the thumbnails
        /// </summary>
        public static string CacheFolder
        {
            get
            {
                return cacheFolder;
            }
            set
            {
                cacheFolder = value;
            }
        }

        public static string NavigationTableClass
        {
            get
            {
                return navigationTableClass;
            }
            set
            {
                navigationTableClass = value;
            }
        }

        public static string ImageTableClass
        {
            get
            {
                return imageTableClass;
            }
            set
            {
                imageTableClass = value;
            }
        }

        /// <summary>
        /// Returns the thumbnail handler file name
        /// </summary>
        public static string ImageHandlerFile
        {
            get
            {
                return imageHandlerFile;
            }
            set
            {
                imageHandlerFile = value;
            }
        }

        /// <summary>
        /// Gets/sets the value if the thumbnails are cached
        /// </summary>
        public static bool CacheThumbnails
        {
            get
            {
                return cacheThumbnails;
            }
            set
            {
                cacheThumbnails = value;
            }
        }

        /// <summary>
        /// Gets/sets the storage provider used by the gallery
        /// </summary>
        public static GalleryStorageProvider StorageProvider
        {
            get
            {
                return storageProvider;
            }
            set
            {
                storageProvider = value;
            }
        }

        /// <summary>
        /// Gets/sets the CSS class name for the category thumb
        /// </summary>
        public static string CategoryThumbClass
        {
            get
            {
                return categoryThumbClass;
            }
            set
            {
                categoryThumbClass = value;
            }
        }

        /// <summary>
        /// Gets/sets the CSS class name for the image thumb
        /// </summary>
        public static string ImageThumbClass
        {
            get
            {
                return imageThumbClass;
            }
            set
            {
                imageThumbClass = value;
            }
        }

        /// <summary>
        /// Gets/sets the main table's class name
        /// </summary>
        public static string MainTableClass
        {
            get
            {
                return mainTableClass;
            }
            set
            {
                mainTableClass = value;
            }
        }

        /// <summary>
        /// Gets/sets the main cell's class name
        /// </summary>
        public static string MainCellClass
        {
            get
            {
                return mainCellClass;
            }
            set
            {
                mainCellClass = value;
            }
        }

        /// <summary>
        /// Gets/sets the folder Url format
        /// </summary>
        public static string FolderUrlFormat
        {
            get
            {
                return folderUrlFormat;
            }
            set
            {
                folderUrlFormat = value;
            }
        }

        /// <summary>
        /// Returns the url format for the images
        /// </summary>
        public static string ImageLinkUrlFormat
        {
            get
            {
                return imageLinkUrlFormat;
            }
            set
            {
                imageLinkUrlFormat = value;
            }
        }

        public static int PreviewImageWidth
        {
            get
            {
                return previewImageWidth;
            }
            set
            {
                previewImageWidth = value;
            }
        }

        #endregion / Public properties /

        #region / Internal helper methods /

        internal static string GetFolderUrl(string folderId)
        {
            return String.Format(CultureInfo.InvariantCulture,
                GalleryManager.FolderUrlFormat, folderId);
        }

        internal static string GetImageLinkUrl(string folderId, string imageName)
        {
            return String.Format(CultureInfo.InvariantCulture,
                GalleryManager.ImageLinkUrlFormat, folderId, imageName);
        }

        internal static string GetImageLinkUrl(FolderImage image)
        {
            if (image == null)
                return null;

            return GetImageLinkUrl(image.ParentFolder, image.ImageName);
        }

        #endregion

        /// <summary>
        /// Returns true if the currently logged in user is an administrator
        /// </summary>
        public static bool IsUserAdmin
        {
            get
            {
                return HttpContext.Current.User.IsInRole(AdminRoleName);
            }
        }

        /// <summary>
        /// Returns the correct css class name
        /// </summary>
        /// <param name="className"></param>
        /// <returns></returns>
        public static string GetCssClass(string className)
        {
            return String.Format("{0}{1}", cssPrefixName, className);
        }
    }
}
